package com.ruoyi.mall.app.service.impl;


import com.ruoyi.mall.app.service.DtsAddressService;
import com.ruoyi.mall.common.domain.DtsAddress;
import com.ruoyi.mall.common.domain.DtsRegion;
import com.ruoyi.mall.common.mapper.DtsAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DtsAddressServiceImpl implements DtsAddressService {

    @Autowired
    private DtsAddressMapper dtsAddressMapper;


    @Override
    public List<DtsAddress> selectAddress(int userId) {
        return dtsAddressMapper.selectAddress(userId);
    }

    @Override
    public DtsAddress selectDefaultAddress(int userId) {
        return dtsAddressMapper.selectDefaultAddress(userId);
    }

    @Override
    public int upDefaultAddress(int userId) {
        return 0;
    }

    @Override
    public List<DtsRegion> selectRegion(DtsRegion dtsRegion) {
        return dtsAddressMapper.selectRegions(dtsRegion);
    }


    @Override
    public int addAddress(DtsAddress address) {
        if (address.getIsDefault() == 1) {
            dtsAddressMapper.upDefaultAddress(address.getUserId());
        }
        //判断有没有默认地址，没有则默认设置为默认地址
        DtsAddress defaultAddress = dtsAddressMapper.selectDefaultAddress(Integer.parseInt(address.getUserId().toString()));
        if (defaultAddress == null ){
            address.setIsDefault(1);
        }

        return dtsAddressMapper.insertDtsAddress(address);
    }

    @Override
    public int upAddress(DtsAddress address) {
        if (address.getIsDefault() == 1) {
            dtsAddressMapper.upDefaultAddress(address.getUserId());
        }
        return dtsAddressMapper.updateDtsAddress(address);
    }

    @Override
    public int delAddress(Long id) {
        return dtsAddressMapper.deleteDtsAddressById(id);
    }

    @Override
    public int delAddress1(DtsAddress dtsAddress) {
        return dtsAddressMapper.delAddress(dtsAddress);
    }
}
